
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Ripyl plotting tutorial &mdash; Ripyl 1.2 documentation</title>
    
    <link rel="stylesheet" href="../_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/ripyl.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="Ripyl 1.2 documentation" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../index.html">Ripyl 1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="ripyl-plotting-tutorial">
<h1>Ripyl plotting tutorial<a class="headerlink" href="#ripyl-plotting-tutorial" title="Permalink to this headline">¶</a></h1>
<p>This tutorial provides an overview of plotting with Ripyl.</p>
<p>The Ripyl library comes with a demo program that can plot examples of each supported protocol. It is possible to use the same plotting facilities to plot your own data if needed. The optional <a class="reference external" href="http://matplotlib.org/">matplotlib</a> library must be installed to plot waveforms with Ripyl.</p>
<div class="section" id="get-sampled-data">
<h2>Get sampled data<a class="headerlink" href="#get-sampled-data" title="Permalink to this headline">¶</a></h2>
<p>For this example we will generate some synthesized I<sup>2</sup>C waveforms to plot with. This step can be skipped if you already have sampled waveforms ready to plot.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ripyl.sigproc</span> <span class="kn">as</span> <span class="nn">sigp</span>
<span class="kn">import</span> <span class="nn">ripyl.streaming</span> <span class="kn">as</span> <span class="nn">stream</span>
<span class="kn">import</span> <span class="nn">ripyl.protocol.i2c</span> <span class="kn">as</span> <span class="nn">i2c</span>

<span class="k">def</span> <span class="nf">sim_i2c</span><span class="p">():</span>
    <span class="c"># I2C params</span>
    <span class="n">clock_freq</span> <span class="o">=</span> <span class="mf">100.0e3</span>

    <span class="c"># Sampled waveform params</span>
    <span class="n">sample_rate</span> <span class="o">=</span> <span class="n">clock_freq</span> <span class="o">*</span> <span class="mf">100.0</span>
    <span class="n">rise_time</span> <span class="o">=</span> <span class="n">sigp</span><span class="o">.</span><span class="n">min_rise_time</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">)</span> <span class="o">*</span> <span class="mf">10.0</span> <span class="c"># 10x min. rise time</span>
    <span class="n">noise_snr</span> <span class="o">=</span> <span class="mf">30.0</span>

    <span class="n">message</span> <span class="o">=</span> <span class="s">&#39;foobar&#39;</span>
    <span class="n">byte_msg</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="n">message</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;latin1&#39;</span><span class="p">))</span> <span class="c"># Get raw bytes as integers</span>

    <span class="n">transfers</span> <span class="o">=</span> <span class="p">[</span><span class="n">i2c</span><span class="o">.</span><span class="n">I2CTransfer</span><span class="p">(</span><span class="n">i2c</span><span class="o">.</span><span class="n">I2C</span><span class="o">.</span><span class="n">Read</span><span class="p">,</span> <span class="mh">0x42</span><span class="p">,</span> <span class="n">byte_msg</span><span class="p">)]</span>

    <span class="c"># Synthesize the waveform edge stream</span>
    <span class="n">scl</span><span class="p">,</span> <span class="n">sda</span> <span class="o">=</span> <span class="n">i2c</span><span class="o">.</span><span class="n">i2c_synth</span><span class="p">(</span><span class="n">transfers</span><span class="p">,</span> <span class="n">clock_freq</span><span class="p">,</span> <span class="n">idle_start</span><span class="o">=</span><span class="mf">3.0e-5</span><span class="p">,</span> <span class="n">idle_end</span><span class="o">=</span><span class="mf">3.0e-5</span><span class="p">)</span>

    <span class="c"># Convert to a sample stream with band-limited edges and noise</span>
    <span class="n">cln_scl_it</span> <span class="o">=</span> <span class="n">sigp</span><span class="o">.</span><span class="n">synth_wave</span><span class="p">(</span><span class="n">scl</span><span class="p">,</span> <span class="n">sample_rate</span><span class="p">,</span> <span class="n">rise_time</span><span class="p">,</span> <span class="n">tau_factor</span><span class="o">=</span><span class="mf">0.7</span><span class="p">)</span>
    <span class="n">cln_sda_it</span> <span class="o">=</span> <span class="n">sigp</span><span class="o">.</span><span class="n">synth_wave</span><span class="p">(</span><span class="n">sda</span><span class="p">,</span> <span class="n">sample_rate</span><span class="p">,</span> <span class="n">rise_time</span><span class="p">,</span> <span class="n">tau_factor</span><span class="o">=</span><span class="mf">1.5</span><span class="p">)</span>

    <span class="c"># Add noise and gain</span>
    <span class="n">noisy_scl_it</span> <span class="o">=</span> <span class="n">sigp</span><span class="o">.</span><span class="n">amplify</span><span class="p">(</span><span class="n">sigp</span><span class="o">.</span><span class="n">noisify</span><span class="p">(</span><span class="n">cln_scl_it</span><span class="p">,</span> <span class="n">snr_db</span><span class="o">=</span><span class="n">noise_snr</span><span class="p">),</span> <span class="n">gain</span><span class="o">=</span><span class="mf">3.3</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
    <span class="n">noisy_sda_it</span> <span class="o">=</span> <span class="n">sigp</span><span class="o">.</span><span class="n">amplify</span><span class="p">(</span><span class="n">sigp</span><span class="o">.</span><span class="n">noisify</span><span class="p">(</span><span class="n">cln_sda_it</span><span class="p">,</span> <span class="n">snr_db</span><span class="o">=</span><span class="n">noise_snr</span><span class="p">),</span> <span class="n">gain</span><span class="o">=</span><span class="mf">3.3</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>

    <span class="c"># Capture the samples from the iterator</span>
    <span class="n">noisy_scl</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">noisy_scl_it</span><span class="p">)</span>
    <span class="n">noisy_sda</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">noisy_sda_it</span><span class="p">)</span>

    <span class="k">return</span> <span class="p">(</span><span class="n">noisy_scl</span><span class="p">,</span> <span class="n">noisy_sda</span><span class="p">)</span>
</pre></div>
</div>
<p>The Ripyl Plotter object needs sample streams as the waveform source(s). If you are getting samples from an external source you must convert them to a <a class="reference internal" href="data_structures.html#streams"><em>sample stream</em></a> with the <a class="reference internal" href="../apidoc/ripyl.html#ripyl.streaming.samples_to_sample_stream" title="ripyl.streaming.samples_to_sample_stream"><tt class="xref py py-func docutils literal"><span class="pre">samples_to_sample_stream()</span></tt></a> function.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Get scl_samples and sda_samples from external source</span>
<span class="n">noisy_scl</span> <span class="o">=</span> <span class="n">stream</span><span class="o">.</span><span class="n">samples_to_sample_stream</span><span class="p">(</span><span class="n">scl_samples</span><span class="p">,</span> <span class="n">sample_period</span><span class="p">)</span>
<span class="n">noisy_sda</span> <span class="o">=</span> <span class="n">stream</span><span class="o">.</span><span class="n">samples_to_sample_stream</span><span class="p">(</span><span class="n">sda_samples</span><span class="p">,</span> <span class="n">sample_period</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="plot-waveforms">
<h2>Plot waveforms<a class="headerlink" href="#plot-waveforms" title="Permalink to this headline">¶</a></h2>
<p>Once the sample streams are prepared you are ready to plot. The StreamRecords produced by the protocol decoders contain annotation information useful for plotting. Ripyl provides a <a class="reference internal" href="../apidoc/ripyl.util.html#ripyl.util.plot.Plotter" title="ripyl.util.plot.Plotter"><tt class="xref py py-class docutils literal"><span class="pre">Plotter</span></tt></a> class that encapsulates a matplotlib figure and handles layout and annotation formatting. You prepare a plot with the <a class="reference internal" href="../apidoc/ripyl.util.html#ripyl.util.plot.Plotter.plot" title="ripyl.util.plot.Plotter.plot"><tt class="xref py py-meth docutils literal"><span class="pre">plot()</span></tt></a> method, providing a dict of channel definitions with the waveform samples, any decoded records for annotation, and the title for the plot. Once the <tt class="docutils literal"><span class="pre">Plotter</span></tt> object is prepared you can either show the result in a matplotlib window or save it to a file.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">matplotlib</span>
<span class="kn">import</span> <span class="nn">ripyl.util.plot</span> <span class="kn">as</span> <span class="nn">rplot</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>

<span class="n">noisy_scl</span><span class="p">,</span> <span class="n">noisy_sda</span> <span class="o">=</span> <span class="n">sim_i2c</span><span class="p">()</span> <span class="c"># Generate simulated sample streams</span>

<span class="c"># The decoded records contain annotation information</span>
<span class="n">records</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">i2c</span><span class="o">.</span><span class="n">i2c_decode</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="n">noisy_scl</span><span class="p">),</span> <span class="nb">iter</span><span class="p">(</span><span class="n">noisy_sda</span><span class="p">)))</span>

<span class="c"># Define the channels ordered from top to bottom with the y-axis labels</span>
<span class="n">channels</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s">&#39;SCL (V)&#39;</span><span class="p">,</span> <span class="n">noisy_scl</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;SDA (V)&#39;</span><span class="p">,</span> <span class="n">noisy_sda</span><span class="p">)])</span>
<span class="n">title</span> <span class="o">=</span> <span class="s">&#39;I2C plot example&#39;</span>

<span class="c"># The Plotter object formats the samples and annotations into plotted waveforms</span>
<span class="n">plotter</span> <span class="o">=</span> <span class="n">rplot</span><span class="o">.</span><span class="n">Plotter</span><span class="p">()</span>
<span class="n">plotter</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">channels</span><span class="p">,</span> <span class="n">records</span><span class="p">,</span> <span class="n">title</span><span class="p">,</span> <span class="n">label_format</span><span class="o">=</span><span class="n">stream</span><span class="o">.</span><span class="n">AnnotationFormat</span><span class="o">.</span><span class="n">Text</span><span class="p">)</span>
<span class="n">plotter</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> <span class="c"># Show the plot in a matplotlib window</span>
</pre></div>
</div>
<p>This produces an interactive plot window:</p>
<a class="reference internal image-reference" href="../_images/plotting_tut1.png"><img alt="../_images/plotting_tut1.png" src="../_images/plotting_tut1.png" style="width: 489.0px; height: 302.25px;" /></a>
<p>You can save the plot to a file instead:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Write PNG image of 8x4 inches (800x400 @ 100dpi)</span>
<span class="n">plotter</span><span class="o">.</span><span class="n">save_plot</span><span class="p">(</span><span class="s">&#39;i2c.png&#39;</span><span class="p">,</span> <span class="p">(</span><span class="mf">8.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">))</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/plotting_tut2.png"><img alt="../_images/plotting_tut2.png" src="../_images/plotting_tut2.png" style="width: 480.0px; height: 240.0px;" /></a>
<p>The <a class="reference internal" href="../apidoc/ripyl.util.html#ripyl.util.plot.Plotter.plot" title="ripyl.util.plot.Plotter.plot"><tt class="xref py py-meth docutils literal"><span class="pre">plot()</span></tt></a> method has a <tt class="docutils literal"><span class="pre">label_format</span></tt> parameter that controls the default format for the annotation text labels. The available formats are defined in the <a class="reference internal" href="../apidoc/ripyl.html#ripyl.streaming.AnnotationFormat" title="ripyl.streaming.AnnotationFormat"><tt class="xref py py-class docutils literal"><span class="pre">AnnotationFormat</span></tt></a> enum:</p>
<dl class="docutils">
<dt>Hidden</dt>
<dd>Invisible text label. Only the colored rectangle is drawn</dd>
<dt>Invisible</dt>
<dd>Invisible text label and rectangle</dd>
<dt>String</dt>
<dd>Record data attribute is treated as a string</dd>
<dt>Text</dt>
<dd>Record data attribute is a sequence of characters</dd>
<dt>Int</dt>
<dd>Label as integers (default)</dd>
<dt>Hex</dt>
<dd>Label as hexadecimal</dd>
<dt>Bin</dt>
<dd>Label as binary</dd>
<dt>Small</dt>
<dd>Same as String but with smaller text</dd>
</dl>
<p>The <tt class="docutils literal"><span class="pre">label_format</span></tt> parameter only affects fields that have been annotated to have a general purpose format. Changing the format to Hex produces the following result. Note that the address portion and the ack bits retain their formatting as String and Hidden.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">plotter</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">channels</span><span class="p">,</span> <span class="n">records</span><span class="p">,</span> <span class="n">title</span><span class="p">,</span> <span class="n">label_format</span><span class="o">=</span><span class="n">stream</span><span class="o">.</span><span class="n">AnnotationFormat</span><span class="o">.</span><span class="n">Hex</span><span class="p">)</span>
</pre></div>
</div>
<a class="reference internal image-reference" href="../_images/plotting_tut3.png"><img alt="../_images/plotting_tut3.png" src="../_images/plotting_tut3.png" style="width: 480.0px; height: 240.0px;" /></a>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/Ripyl logo 100px.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Ripyl plotting tutorial</a><ul>
<li><a class="reference internal" href="#get-sampled-data">Get sampled data</a></li>
<li><a class="reference internal" href="#plot-waveforms">Plot waveforms</a></li>
</ul>
</li>
</ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/rst/tut_plot.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../index.html">Ripyl 1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2013, Kevin Thibedeau.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>

<script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-43149311-1', 'google.com');
  ga('send', 'pageview');

</script>

  </body>
</html>